package com.wm.blog_admin.task;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_admin.controller.resp.AdminExportResp;
import com.wm.blog_admin.query.TAdminQuery;
import com.wm.blog_admin.service.TAdminService;
import com.wm.blog_common.base.BeanUtils;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.domain.TAdminDO;
import com.wm.blog_common.result.Result;
import lombok.AllArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

/**
 * @author 半卷流年
 * @date 2020-5-8 13:49
 */
@AllArgsConstructor
public class AdminThreadTask implements Callable<List<AdminExportResp>> {

    private Integer current;

    private Integer size;

    private TAdminService tAdminService;

    private TAdminQuery query;

    @Override
    public List<AdminExportResp> call() throws Exception {
        QueryPage page = new QueryPage(current, size);
        Result<Page<TAdminDO>> result = tAdminService.page(page, query);
        List<AdminExportResp> respList = new ArrayList<>();
        if(result.isSuccess() && !Objects.isNull(result.getData()) && !CollectionUtils.isEmpty(result.getData().getRecords())){
            List<AdminExportResp> list = result
                    .getData()
                    .getRecords()
                    .stream()
                    .map(x -> {
                AdminExportResp resp = new AdminExportResp();
                BeanUtils.copyProperties(x, resp);
                return resp;
            })
                    .collect(Collectors.toList());
            respList.addAll(list);
        }
        return respList;
    }
}
